package com.lxr0860.lxr0860production_test;

import static android.os.Environment.isExternalStorageEmulated;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.DhcpInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.RequiresApi;

<<<<<<< HEAD:app/src/main/java/com/lxr0860/lxr0860production_test/ConfigActivity.java
import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.adapter.ConfigAdapter;
import com.lxr0860.lxr0860production_test.bean.ConfigItem;
import com.lxr0860.lxr0860production_test.utils.Contants;
import com.lxr0860.lxr0860production_test.utils.LogcatHelper;
import com.lxr0860.lxr0860production_test.utils.SharedPreferencesUtils;
import com.lxr0860.lxr0860production_test.utils.ToastUtils;
=======
import com.lxr5002.lxr5002production_test.adapter.ConfigAdapter;
import com.lxr5002.lxr5002production_test.bean.ConfigItem;
import com.lxr5002.lxr5002production_test.utils.Contants;
import com.lxr5002.lxr5002production_test.utils.LogcatHelper;
import com.lxr5002.lxr5002production_test.utils.SharedPreferencesUtils;
import com.lxr5002.lxr5002production_test.utils.ToastUtils;
import com.sdk.devicemanager.ICcon;
>>>>>>> e4f272b5472579b3974abdf8f4069581f4fe75a7:app/src/main/java/com/lxr5002/lxr5002production_test/ConfigActivity.java

import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by uniriho on 2018/9/3.
 */

public class ConfigActivity extends Activity{
    private ListView mLvConfigList;
    private Button mBtnConfigOk;
    private ConfigAdapter mAdapter;
    private List<ConfigItem> configs;

<<<<<<< HEAD:app/src/main/java/com/lxr0860/lxr0860production_test/ConfigActivity.java
=======

>>>>>>> e4f272b5472579b3974abdf8f4069581f4fe75a7:app/src/main/java/com/lxr5002/lxr5002production_test/ConfigActivity.java
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_config);
        initView();
        initData();
        initEvent();
        ICcon.getInstance().debugMode(true);

        requestManagerPermission();
        LogcatHelper.getInstance().start();
        Log.d("logcat","start save");

    }

<<<<<<< HEAD:app/src/main/java/com/lxr0860/lxr0860production_test/ConfigActivity.java
    /**
     * 板卡热检测
     * @param key  persist.sys.pciekeyenable
     * @param value  true：打开检测    false:关闭检测
     */
    public void setProp(String key, String value) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, value );
            Log.e("setProp","success");
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("setProp",e.toString());
        }
    }


    /**
     * 板卡热检测
     * @param key  persist.sys.pciekeyenable
     * @param value  true：打开检测    false:关闭检测
     */
    public void setSettings(String key, String value) {
        try {
            Class<?> c = Class.forName("android.provider.Settings");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, value );
            Log.e("setSettings","success");
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("setSettings",e.toString());
        }
    }


    /**
     * 获取板卡热检测 状态
     * @param key  persist.sys.pciekeyenable
     * return value  true：打开检测    false:关闭检测
     */
    public  String getProp(String key) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            Object invoke = get.invoke(c, key);
            Log.e("getProp",invoke.toString());
            return invoke.toString();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("getProp",e.toString());
            return e.toString();
        }
    }

    public static void setProp2(String key, String value) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, value );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
=======
>>>>>>> e4f272b5472579b3974abdf8f4069581f4fe75a7:app/src/main/java/com/lxr5002/lxr5002production_test/ConfigActivity.java

    private void initView() {
        mLvConfigList = (ListView) findViewById(R.id.lv_config_list);
        mBtnConfigOk = (Button) findViewById(R.id.btn_config_ok);
    }

    private void initData() {
        configs = new ArrayList<>();
        for (String configName : Contants.configs) {
            configs.add(new ConfigItem(configName, SharedPreferencesUtils.getBooleanData(this,configName, false)));
        }

        mAdapter = new ConfigAdapter(this, configs);
        mLvConfigList.setAdapter(mAdapter);
<<<<<<< HEAD:app/src/main/java/com/lxr0860/lxr0860production_test/ConfigActivity.java
        getPingMuSize(this);
=======
        setProp("persist.sys.pciekeyenable","false");
>>>>>>> e4f272b5472579b3974abdf8f4069581f4fe75a7:app/src/main/java/com/lxr5002/lxr5002production_test/ConfigActivity.java
    }

    private void initEvent() {
        mLvConfigList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                configs.get(position).setConfigChosed(!configs.get(position).isConfigChosed());
                mAdapter.notifyDataSetChanged();
            }
        });

        mBtnConfigOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (ConfigItem config : configs) {
                    SharedPreferencesUtils.saveBooleanData(ConfigActivity.this,config.getConfigName(), config.isConfigChosed());
                }
                Intent intent = new Intent(ConfigActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLvConfigList != null) {
            mLvConfigList = null;
        }
        if (configs != null) {
            configs.clear();
            configs = null;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void requestManagerPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 先判断有没有权限
            if (!isExternalStorageEmulated()) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 100);
            }
        }
    }

    @SuppressLint("DefaultLocale")
    public void getApIpAddress2() {
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        DhcpInfo info=wifiManager.getDhcpInfo();
        int ipAddress = info.ipAddress;
        int serverAddress = info.serverAddress;
        Log.e("ipAddress",String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)));
        Log.e("serverAddress",String.format("%d.%d.%d.%d", (serverAddress & 0xff), (serverAddress >> 8 & 0xff), (serverAddress >> 16 & 0xff), (serverAddress >> 24 & 0xff)));
    }

    /**
<<<<<<< HEAD:app/src/main/java/com/lxr0860/lxr0860production_test/ConfigActivity.java
     * @ 获取当前手机屏幕的尺寸(单位:像素)
     */
    public void getPingMuSize(Context mContext) { int densityDpi = mContext.getResources().getDisplayMetrics().densityDpi;
        float scaledDensity = mContext.getResources().getDisplayMetrics().scaledDensity;
        float density = mContext.getResources().getDisplayMetrics().density;
        float xdpi = mContext.getResources().getDisplayMetrics().xdpi;
        float ydpi = mContext.getResources().getDisplayMetrics().ydpi;
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        int height = mContext.getResources().getDisplayMetrics().heightPixels;

        // 这样可以计算屏幕的物理尺寸
        float width2 = (width / xdpi)*(width / xdpi);
        float height2 = (height / ydpi)*(width / xdpi);
        Log.e("getPingMuSize_xdpi",xdpi+"");
        Log.e("getPingMuSize_ydpi",ydpi+"");
        Log.e("getPingMuSize_width",width+"");
        Log.e("getPingMuSize_height",height+"");

=======
     * 板卡热检测
     * @param key ： persist.sys.pciekeyenable
     * @param value ： true：打开检测    false:关闭检测
     */
    public static void setProp(String key, String value) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, value );
            ToastUtils.showMessage("控制卡热检测关闭成功");
        } catch (Exception e) {
            ToastUtils.showMessage("控制卡热检测关闭失败");
            e.printStackTrace();
            Log.e("setProp",e.toString());
        }
>>>>>>> e4f272b5472579b3974abdf8f4069581f4fe75a7:app/src/main/java/com/lxr5002/lxr5002production_test/ConfigActivity.java
    }
}
